package com.div.tools.ui.main;

import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

import com.div.tools.ConfigInfo;
import com.div.tools.collection.CollectionActivity;
import com.div.tools.popup.PopupWindowActivity;
import com.div.tools.rating.RatingBarActivity;
import com.div.tools.screen.ScreenActivity;
import com.div.tools.toast.ToastActivity;
import com.div.tools.viewpager.ViewPagerActivity;

import java.util.ArrayList;
import java.util.List;

public class PageViewModel extends ViewModel {

    private String [][] titles = new String[][]{
            {"ToastTool", "FileTool"},
            {"PopupWindow", "ViewPager", "Screen", "RatingBar"},
            {"Collection"}
    };
    private Class[][] clazz = new Class[][] {
            {ToastActivity.class, ToastActivity.class},
            {PopupWindowActivity.class, ViewPagerActivity.class, ScreenActivity.class,
                    RatingBarActivity.class},
            {CollectionActivity.class}
    };
    private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
    private LiveData<List<ConfigInfo>> mList = Transformations.map(mIndex, new Function<Integer, List<ConfigInfo>>() {
        @Override
        public List<ConfigInfo> apply(Integer input) {
            if (input >= titles.length) {
                return null;
            }
            List<ConfigInfo> list = new ArrayList<>();
            for (int i = 0; i < titles[input].length; i++) {
                ConfigInfo configInfo = new ConfigInfo();
                configInfo.setItemName(titles[input][i]);
                configInfo.setActivity(clazz[input][i]);
                list.add(configInfo);
            }

            return list;
        }
    });

    public void setIndex(int index) {
        mIndex.setValue(index);
    }

    public LiveData<List<ConfigInfo>> getText() {
        return mList;
    }
}